package com.cryptape.cita_wallet.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.blankj.utilcode.util.ObjectUtils;
import com.cryptape.cita_wallet.bean.BindHashBean;
import com.cryptape.cita_wallet.constant.ConstantUtil;
import com.cryptape.cita_wallet.item.transaction.BaseTransaction;
import com.cryptape.cita_wallet.item.transaction.RestTransaction;
import com.mangoworld.android.R;

import java.util.List;

;

/**
 * Created by BaojunCZ on 2018/10/9.
 */
public class BindHashRecordAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int VIEW_TYPE_EMPTY = 0;
    private static final int VIEW_TYPE_ITEM = 1;
    private static final int VIEW_TYPE_LOADING = 2;

    private List<BindHashBean> bindHashBeanList;
    private Context context;

    private OnItemClickListener onItemClickListener;

    public BindHashRecordAdapter(Context context, List<BindHashBean> bindHashBeanList) {
        this.bindHashBeanList = bindHashBeanList;
        this.context = context;
    }

    public void refresh(List<BindHashBean> bindHashBeanList) {
        this.bindHashBeanList = bindHashBeanList;
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void addLoadingView() {
        if (bindHashBeanList.size() > 0) {
            bindHashBeanList.add(null);
            notifyItemInserted(bindHashBeanList.size() - 1);
        }
    }

    public void removeLoadingView() {
        if (bindHashBeanList.size() > 0) {
            bindHashBeanList.remove(bindHashBeanList.size() - 1);
            notifyItemRemoved(bindHashBeanList.size());
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == VIEW_TYPE_EMPTY) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_empty_view, parent, false);
            ((TextView) view.findViewById(R.id.empty_text)).setText(R.string.empty_no_transaction_data);
            return new RecyclerView.ViewHolder(view) {
            };
        } else if (viewType == VIEW_TYPE_LOADING) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.progress_loading, parent, false);
            return new RecyclerView.ViewHolder(view) {
            };
        } else {
            return new TransactionViewHolder(LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_transaction_list, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof TransactionViewHolder) {
            BindHashBean bindHashBean = bindHashBeanList.get(position);
            TransactionViewHolder viewHolder = (TransactionViewHolder) holder;
            viewHolder.transactionToAddressText.setText(ObjectUtils.isEmpty(bindHashBean.getHash()) ? "" : bindHashBean.getHash());
            viewHolder.transactionAmountText.setText(ObjectUtils.isEmpty(bindHashBean.getApplyNum()) ? "0" : String.valueOf(bindHashBean.getApplyNum()));
            viewHolder.transactionTimeText.setText(ObjectUtils.isEmpty(bindHashBean.getCreateTime()) ? "" : bindHashBean.getCreateTime());
            switch (bindHashBean.getStatus()) {//状态 0 待映射 1 完成 2 失败 3 失败 4 撤销
                case 0:
                    viewHolder.transactionStatus.setText(R.string.str_need_map);
                    viewHolder.transactionStatus.setTextColor(context.getResources().getColor(R.color.red));
                    break;
                case 1:
                    viewHolder.transactionStatus.setText(R.string.finish);
                    viewHolder.transactionStatus.setTextColor(context.getResources().getColor(R.color.assist_color));
                    break;
                case 4:
                    viewHolder.transactionStatus.setText(R.string.str_withdraw);
                    viewHolder.transactionStatus.setTextColor(context.getResources().getColor(R.color.warning_font));
                    break;
                default:
                    viewHolder.transactionStatus.setText(R.string.str_failure);
                    viewHolder.transactionStatus.setTextColor(context.getResources().getColor(R.color.font_title_third));
                    break;
            }
            viewHolder.itemView.setTag(position);
        }
    }

    @Override
    public int getItemCount() {
        if (bindHashBeanList.size() == 0) {
            return 1;
        }
        return bindHashBeanList.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (bindHashBeanList.size() == 0) {
            return VIEW_TYPE_EMPTY;
        } else if (bindHashBeanList.get(position) == null) {
            return VIEW_TYPE_LOADING;
        }
        return VIEW_TYPE_ITEM;
    }

    class TransactionViewHolder extends RecyclerView.ViewHolder {
        TextView transactionToAddressText;
        TextView transactionAmountText;
        TextView transactionTimeText;
        TextView transactionStatus;

        public TransactionViewHolder(View view) {
            super(view);
            view.setOnClickListener(v -> {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(v, (int) v.getTag());
                }
            });
            transactionToAddressText = view.findViewById(R.id.transaction_to_address_text);
            transactionTimeText = view.findViewById(R.id.transaction_time_text);
            transactionAmountText = view.findViewById(R.id.transaction_amount);
            transactionStatus = view.findViewById(R.id.transaction_status);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }
}
